 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { TaskHelper } from "utils/task/TaskHelper";
import { generateUniqueId } from "utils/utils";

/**
 * 升级任务决策
 */
export class UpgradeDecision< T extends "creep"="creep"> extends ABaseDecision<T>
{
   
    public isExpired(event: NormalEvent, region: string): boolean 
    {
        return true;
    
    }
    public makeTask(event: NormalEvent, region: string): IDecisionCode<T>|false
    {
        global.Log.Info(`更新升级任务 ${JSON.stringify(event)}`);
        const room = Game.rooms[event.region];
        const controller = room.controller!;
        const core = room.getEntity("CoreEntity");
        const contanier = controller?.getContainer();
        if(((contanier && contanier.length)|| core.Container) && room.controller!.level <8)
        {
            this.deleteUpgradeTask(room,"creep");
            const teamTask =  this.createTeamUpgradeTask(room);
            
            const code:IDecisionCode<T>={
                region:event.region,
                isunique:false,
                task:[
                    teamTask as any
                ]
            }
            return code;
        }
        else
        {
            this.deleteUpgradeTask(room,"team");
            const creepTask = this.createCreepUpgradeTask(room);
            const code:IDecisionCode<T>={
                region:event.region,
                isunique:false,
                task:[
                    creepTask
                ]
            }
            return code;
        }


    }
    private deleteUpgradeTask(room:Room,type:TaskType)
    {
        const upgradeTask = room.getTaskByType(type,(e:ITaskDeclare[TaskType])=>e.data.type == "upgrade")[0];
        if(upgradeTask)
        {
            room.deleteTaskById(upgradeTask.id,type);
        }
    }
    private createCreepUpgradeTask(room:Room)
    {
        const keepTask = room.getTaskByType("KeepCreep")[0];
        const roleConfig = keepTask.data.role.find(e=>e[0].Role == "upgrader") as  [IBodyConfig, number] ;
        // 根据升级共数量。计算运输工数量
        const controller =  room.controller!;
        let maxcount=15;
        if(controller.level>2)
        {
            if(controller.level==8)
            {
                maxcount = 1;
            }
            else 
            {
                const body = getRoleConfig("upgrader",controller.level);
                maxcount = BodyUtils.getCreepCount(body.body,WORK,controller.getWorkBodyNum());
            }
            
        }
        else 
        {
            maxcount=4;
        }
        const upgradeTask = room.getTaskByType("creep",(e:ITaskDeclare["creep"])=>e.data.type == "upgrade")[0];
        if(upgradeTask)
        {
            upgradeTask.data.arg = {room:room.name};
            upgradeTask.data.required = [[["upgrader",'worker'], Math.min( roleConfig[1],maxcount),Math.min( roleConfig[1],maxcount)]]
            return upgradeTask
        }
        else
        {
            const task:ITaskDeclare[T]={
                id:generateUniqueId(),
                priority:priority.F,
                ttl:-1,
                type:"creep",
                data:{
                    type:"upgrade",
                    arg:{room:room.name},
                    creeps:[],
                    room:room.name,
                    body:{},
                    income:-1,
                    cost:TaskHelper.getTaskPathCost(room.name,room.name),
                    required:[
                        [["upgrader",'worker'],  Math.min( roleConfig[1],maxcount), Math.min( roleConfig[1],maxcount)]
                    ]
                },
                shard: Math.min( roleConfig[1],maxcount),
                
            }
            return task
        }
    }
    private createTeamUpgradeTask(room:Room)
    {
        const upgradeTask = room.getTaskByType("team",(e:ITaskDeclare["team"])=>e.data.type == "upgrade")[0];
        if(upgradeTask)
        {
            
            return upgradeTask
        }
        const keepTask = room.getTaskByType("KeepCreep")[0];
        const roleConfig = keepTask.data.role.find(e=>e[0].Role == "upgrader") as  [IBodyConfig, number] ;
        const body = getRoleConfig("upgrader",room.controller!.level);
        const    maxcount = BodyUtils.getCreepCount(body.body,WORK,room.controller!.getWorkBodyNum());
        
        let newtask:ITask<ITeamTaskData<"upgrade">> ={
            id:generateUniqueId(),
            ttl:-1,
            type:"team",
            data:{
                type:"upgrade",
                room:room.name,
                arg:{ },
                teams:{} as any,
                required:[
                   [{
                    type:"TeamUpgrader",
                    creep:[
                        [{
                          Role:"upgrader" ,
                          body:getRoleConfig("upgrader",room.controller?.level as number).body,
                        }
                        ,Math.round( Math.min( roleConfig[1],maxcount)/2)
                        ]
                    ]
                   },1]
                ],
                cost:TaskHelper.getTaskPathCost(room.name,room.name),
                creepRequired:[],
                creeps:[]
            },
    
            priority:priority.F,
            shard: 0,
            
        }
        return newtask;
    }
}